<template>
	<!-- 陆亿版本 -->
	<view>
		<!-- 头部搜索框 -->
		<search :placeholder="$t('homepage.FabricReport.volumeNo')" />
		<view class="total">
			<view v-for="(m,n) in list" :key="n" class="item"
				:style="[{borderRight:`${n==list.length-1?'':'6rpx dashed #222'}`}]">
				<span style="font-weight: 800;">{{m.num}}</span></br>
				<span style="color: #999999;font-size: 30rpx;">{{m.title}}</span>
			</view>
		</view>
		<!-- 搜索无记录 -->
		<zy-empty v-if="dataList.length==0"></zy-empty>
		<!-- 明细列表 -->
		<zy-group v-for="(item,index) in dataList" :key="item.id" model="insideCard" v-else>
			<zy-forms :model="item" border labelWidth="160">
				<zy-form-item :label="$t('homepage.ProductionMonitor.lightNoTitle')"><text
						class="formText">{{item.sgh || $t('public.null')}}</text></zy-form-item>
				<zy-form-item :label="$t('homepage.WarehouseManage.newlLightNo') + $t('public.colon')" required>
					<input type="text" v-model="item.newSgh" :placeholder="$t('public.pleaseInput')+'新色光号'" />
				</zy-form-item>
				<zy-form-item :label="$t('homepage.ProductionMonitor.volumeNoTitle')"><text
						class="formText">{{item.barcode || $t('public.null')}}</text>
				</zy-form-item>
				<zy-form-item :label="$t('homepage.ProductionMonitor.materialCodeTitle')"><text
						class="formText">{{item.materialCode || $t('public.null')}}</text>
				</zy-form-item>
				<zy-form-item :label="$t('public.name') + $t('public.colon')"><text
						class="formText">{{item.materialName || $t('public.null')}}</text>
				</zy-form-item>
				<zy-form-item :label="$t('public.materialSpec') + $t('public.colon')"><text
						class="formText">{{item.materialSpec || $t('public.null')}}</text>
				</zy-form-item>
				<zy-form-item :label="$t('homepage.ProductionMonitor.colorTitle')"><text
						class="formText">{{item.color || $t('public.null')}}</text>
				</zy-form-item>
				<zy-form-item :label="$t('homepage.WarehouseManage.productDoorWidth') + $t('public.colon')"><text
						class="formText">{{item.mengfucM || $t('public.null')}}</text>
				</zy-form-item>
				<zy-form-item :label="$t('homepage.WarehouseManage.productGramWeight') + $t('public.colon')"><text
						class="formText">{{item.cpkz || $t('public.null')}}</text>
				</zy-form-item>
				<zy-form-item :label="$t('homepage.ProductionMonitor.numTitle')"><text
						class="formText">{{item.num || $t('public.null')}}</text>
				</zy-form-item>
				<zy-form-item :label="$t('homepage.WarehouseManage.clothDoorWidth') + $t('public.colon')"><text
						class="formText">{{item.hxmf || $t('public.null')}}</text>
				</zy-form-item>
				<zy-form-item :label="$t('homepage.WarehouseManage.clothGramWeight') + $t('public.colon')"><text
						class="formText">{{item.pibukezhong || $t('public.null')}}</text>
				</zy-form-item>
				<zy-form-item :label="$t('homepage.WarehouseManage.flowerPatternName') + $t('public.colon')"><text
						class="formText">{{item.hxmc || $t('public.null')}}</text>
				</zy-form-item>
				<zy-form-item :label="$t('homepage.WarehouseManage.colorCode') + $t('public.colon')"><text
						class="formText">{{item.hxbm || $t('public.null')}}</text>
				</zy-form-item>
				<zy-form-item :label="$t('public.batchNo') + $t('public.colon')"><text
						class="formText">{{item.batchNo || $t('public.null')}}</text>
				</zy-form-item>
			</zy-forms>
		</zy-group>
		<!-- 底部按钮 -->
		<bottomBtn fixed :leftText="$t('public.reset')" :rightText="rightText" :leftDisabled="disabled"
			:rightDisabled="disabled" @rightBtn="rightBtn" @leftBtn="resetBtn">
		</bottomBtn>
		<!-- 仓储位选择弹窗 -->
		<zy-popup ref="popup" type='dialog' :title="$t('homepage.WarehouseManage.SPselect')" @confirm="selectConfirm"
			:closeOnClickOverlay="false" @cancel="selectCancel">
			<zy-forms :rules="selectRules" :model="selectModel" labelWidth="80">
				<zy-form-item :label="$t('public.storage')" required>
					<zy-input v-model="selectModel.storage" :placeholder="$t('public.pleaseInput')+$t('public.storage')"
						@confirm="storageSearch" sufSlot>
						<i class="iconfont icon-saoyisao" @click="scanStorage"></i>
					</zy-input>
				</zy-form-item>
				<zy-form-item :label="$t('public.position')" required>
					<zy-input v-model="selectModel.position" @confirm="positionSearch"
						:placeholder="$t('public.pleaseInput')+$t('public.position')" sufSlot>
						<i class="iconfont icon-saoyisao" @click="scanPosition"></i>
					</zy-input>
				</zy-form-item>
			</zy-forms>
		</zy-popup>
		<!-- 提交二次确认 -->
		<zy-popup ref="confirmPopup" :title="$t('homepage.WarehouseManage.submitSure')" type="dialog"
			@confirm="submitConfirm">
			{{$t('homepage.WarehouseManage.submitInfo')}}
		</zy-popup>
	</view>
</template>
<script>
	import {
		getStorehouseInByCode,
		positionGetEntityByCode,
		storageGetEntityByCode,
		inStorehouseIn
	} from "@/api/homepage.js"
	import scanData from '@/common/scan.js';
	import audio from '@/common/audio.js' // 语音播报
	export default {
		data() {
			return {
				list: [{
					num: "0",
					title: this.$t('homepage.FabricReport.volumeNo')
				}, {
					num: "0",
					title: this.$t('homepage.WarehouseManage.meterTotal')
				}],
				dataList: [],
				disabled: true, //底部按钮禁用
				rightText: this.$t('homepage.WarehouseManage.SPselect'), //底部右侧按钮名称
				// 仓储位选择弹窗表单
				selectModel: {
					position: '',
					storage: '',
				},
				storageId: "",
				positionId: "",
				selectRules: {
					position: {
						required: false,
						message: this.$t('public.pleaseInput') + this.$t('public.position'),
						trigger: 'blur'
					},
					storage: {
						required: true,
						message: this.$t('public.pleaseInput') + this.$t('public.storage'),
						trigger: 'blur'
					}
				},
			}
		},
		onLoad() {
			this.getscancodedate()
		},
		onUnload() {
			// 移除监听事件      
			uni.$off('scancodedate')
			uni.$off('scancodepopdate')
		},
		watch: {
			dataList: {
				handler(n, o) {
					//根据明细清单列表长度,判断底部左侧按钮是否禁用
					if (n.length > 0) {
						this.disabled = false
					} else {
						this.disabled = true
					}
				},
				immediate: true,
				deep: true
			},
		},
		methods: {
			//主页获取箱号扫描事件
			getscancodedate() {
				// 移除监听“添加扫描”弹窗事件
				uni.$off('scancodepopdate')
				//PDA扫描主页指示单号
				var _this = this
				uni.$on('scancodedate', function(data) {
					_this.searchList(data.code)
				})
			},
			//“弹窗储位”获取扫描事件
			getscancodepopdate() {
				// 移除监听“指示单号”事件
				uni.$off('scancodedate')
				//PDA扫描“添加扫描”弹窗事件
				var _this = this
				uni.$on('scancodepopdate', function(data) {
					_this.selectModel.storage = data.code
					_this.storageSearch()
				})
			},
			// 查询事件
			searchList(e) {
				getStorehouseInByCode({
					barCode: e,
				}).then((res) => {
					if (res.code == 0) {
						if (this.dataList.findIndex(item => item.barcode === e) !== -1) {
							uni.showToast({
								title: this.$t('public.searchRepeat'),
								icon: "none",
								duration: 3000
							})
							audio.getInnerAudio('../../../../../static/repeat.wav')
						} else {
							let data = res.data
							data.forEach(item => {
								this.$set(item, 'newSgh', '')
							})
							this.dataList = this.dataList.concat(data)
							this.list[0].num = this.dataList.length
							this.list[1].num = this.dataList.reduce((prev, curr) => {
								return prev + curr.num
							}, 0)
							audio.getInnerAudio('../../../../../static/scanSuccess.wav')
						}
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
						audio.getInnerAudio('../../../../../static/scanFail.wav')
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
					audio.getInnerAudio('../../../../../static/scanFail.wav')
				})
			},
			// 重置事件
			resetBtn() {
				uni.redirectTo({
					url: "/pages/HomePage/WarehouseManage/StorageManagement/Instorage/index"
				})
			},
			// 底部右侧按钮事件
			rightBtn(item) {
				this.$refs.popup.open()
				this.getscancodepopdate()
			},
			//仓位扫描事件
			scanPosition() {
				scanData.getScanData()
				uni.$once("barScan", (rel) => {
					this.selectModel.position = rel;
					this.positionSearch()
				})
			},
			//储位扫描事件
			scanStorage() {
				scanData.getScanData()
				uni.$once("barScan", (rel) => {
					this.selectModel.storage = rel;
					this.storageSearch()
				})
			},
			//仓位接口
			positionSearch() {
				positionGetEntityByCode({
					code: this.selectModel.position
				}).then((res) => {
					if (res.code == 0) {
						this.positionId = res.data.id
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((res) => {
					console.log(res)
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
			//储位接口
			storageSearch() {
				storageGetEntityByCode({
					code: this.selectModel.storage
				}).then((res) => {
					if (res.code == 0) {
						if (res.data.warehousePosition !== null) {
							this.selectModel.position = res.data.warehousePosition.code
							this.positionSearch()
							this.storageId = res.data.id
						} else {
							uni.showToast({
								title: this.$t('homepage.WarehouseManage.nullPosition'),
								icon: "none",
								duration: 3000
							})
							this.selectModel.storage = ""
							this.selectModel.position = ""
						}
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
			// 仓储位弹窗取消
			selectCancel() {
				let that = this
				that.getscancodedate()
			},
			// 仓储位弹窗提交
			selectConfirm() {
				let that = this
				that.$refs.confirmPopup.open()
				that.dataList.forEach((item, index) => {
					item.warehousePositionId = that.positionId
					item.warehouseStorageId = that.storageId
				})
			},
			// 提交二次确认弹窗确认事件
			submitConfirm() {
				this.$refs.popup.close()
				this.$refs.confirmPopup.close()
				inStorehouseIn({
					dataArray: JSON.stringify(this.dataList)
				}).then((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
					if (res.code == 0) {
						setTimeout(() => {
							this.resetBtn()
						}, 1000)
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
		}
	}
</script>
<style lang="scss" scoped>
	.total {
		width: 100%;
		margin: 10rpx 0 20rpx 0;
		display: flex;

		.item {
			padding: 10rpx 0;
			width: 50%;
			margin: auto;
			text-align: center;
		}
	}
</style>